package com.duoduo.controller.admin;

import com.duoduo.constant.JwtClaimsConstant;
import com.duoduo.dto.EmployeeDTO;
import com.duoduo.dto.EmployeeLoginDTO;
import com.duoduo.dto.EmployeePageQueryDTO;
import com.duoduo.entity.Employee;
import com.duoduo.properties.JwtProperties;
import com.duoduo.result.PageResult;
import com.duoduo.result.Result;
import com.duoduo.service.EmployeeService;
import com.duoduo.utils.JwtUtil;
import com.duoduo.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")  // 通过Swagger注解可以控制生成的接口文档描述，使接口文档拥有更好的可读性
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        System.out.println("当前线程id：" + Thread.currentThread().getId());

        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()  // 通过builder()创建对象（前提是EmployeeLoginVO类有@builder注解）
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @return
     */
    @PostMapping
    @ApiOperation(value ="新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO){   // 用@RequestBody接受前端传来的json参数，并封装为DTO对象
        log.info("新增员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     */
    @GetMapping("page")
    @ApiOperation(value = "员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){  // 封装前端Query请求(url,非json)传来的name，page，pageSize
        log.info("员工分页查询，参数：{}",employeePageQueryDTO);
        PageResult pageResult =  employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用或禁用员工账号
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value ="启用或禁用员工账号")
    public Result startOrStop(@PathVariable Integer status, Long id){  // status是路径参数，id是Query地址栏参数
        log.info("启用或禁用员工账号：{}，{}",status,id);
        employeeService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改员工-回显
     */
    @GetMapping("/{id}")
    @ApiOperation("修改员工-回显（根据id查询员工信息）")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("修改员工-回显（根据id查询员工信息）:{}",id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){  // 使用DTO对象封装前端传来的参数
        log.info("编辑员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
